package org.hackystat.devcathlon.ui.page.profile;

import static org.junit.Assert.*;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.hackystat.devcathlon.engine.test.DevcathlonTestHelper;
import org.hackystat.devcathlon.ui.DevcathlonApplication;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.login.LoginPage;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Test Profile Panel class.
 * @author Anthony Du
 *
 */
public class TestProfilePanel extends DevcathlonTestHelper {

  private WicketTester tester;
  private static final String teamName = "TestTeam";
  private static final String owner = "admin";
  private static final String user = "admin@hackystat.org";
  private static final String password = "admin@hackystat.org";
  private static final String[] members = {"member1", "member2"};

  /**
   * Setup Profile session.
   * @throws Exception when an error occurs
   */
  @Before
  public void setUp() throws Exception {
    tester = new WicketTester(new DevcathlonApplication());
    tester.startPage(ProfilePage.class);
    tester.assertRenderedPage(LoginPage.class);
    // login
    FormTester formTester = tester.newFormTester("loginForm");
    formTester.setValue("user", user);
    formTester.setValue("password", password);
    formTester.submit();
    // render Profile main page
    tester.assertRenderedPage(ProfilePage.class);
    // create team & project
    makeTeam(teamName, owner, members);
  }

  /**
   * Cleans up initialized variables.
   * @throws Exception when an error occurs
   */
  @After
  public void cleanUp() throws Exception {
    deleteTeam(teamName);
  }

  /**
   * Test getting list of profile teams.
   * @throws Exception when an error occurs 
   */
  @Test
  public void testTeamProfileLinks() throws Exception {
    ProfileSession session = DevcathlonSession.get().getProfileSession();
    //test have team(s)
    assertNotNull("teams not null", session.getTeams());
    assertFalse("teams list should contain team(s)", session.getTeams().isEmpty());
    assertEquals("number of teams should be 1", 1, session.getTeams().size());
    assertNotNull("should have teams panel", tester.getTagByWicketId("teamsPanel"));
    assertNotNull("should have team profile link", tester.getTagByWicketId("boxContent"));
    assertEquals("should be the same profile", session.getProfile().getUser().getEmail(),
            DevcathlonSession.get().getEmail());
    tester.assertLabel("welcomeMessage", "Welcome home "
            + session.getProfile().getScreenName() + "!");
//    tester.assertLabel("profilePanel:teamsPanel:boxContent:teams:0:teamProfileLink:teamName",
//            teamName);
//    tester.clickLink("teamProfileLink");
//    tester.assertRenderedPage(TeamsProfile.class);
  }
  /**
   * Test avatar.
   * @throws ConfigurationException when an error occurs
   */
//  @Test
//  public void testAvatar() throws ConfigurationException {
//    tester.assertRenderedPage(ProfilePage.class);
//    assertNotNull("test avatar exists", tester.getTagByWicketId("avatar"));
//  }
}
